<?php

namespace common\logics\order;

use Yii;
use yii\helpers\Inflector;
use common\models\shop\ShopStore;

/**
 * 统一订单逻辑模型
 * 
 * @author emhome<emhome@163.com>
 * @since 2.0
 */
class OrderUnifiedLogic extends \common\models\shop\ShopOrderUnified {

    /**
     * 订单操作类型
     * @var string
     */
    const HANDLE_CANCEL = 'cancel';
    const HANDLE_RECEIVE = 'receive';

    /**
     * 订单操作
     */
    use OrderHandleTrait;

    /**
     * {@inheritdoc}
     */
    public function getHandleButtons() {
        return [
            'delete',
            'cancel',
            'pay',
            'receive',
            'evaluate',
            'service',
        ];
    }

    /**
     * 订单状态文本
     * @return string
     */
    public function getStatusText() {
        if ($this->status == static::STATUS_CLOSE) {
            return '已关闭';
        }
        if ($this->status == static::STATUS_CANCEL) {
            return '已取消';
        }
        if ($this->isPay) {
            if ($this->status >= static::STATUS_SHIP && $this->status <= static::STATUS_SHIPPED) {
                return '待收货';
            } elseif ($this->status >= static::STATUS_RECEIVED) {
                return '已完成';
            }
            return '已付款';
        }
        if (!$this->isExpired) {
            return '等待付款';
        }
        return '已取消';
    }

    public function getStatusColor() {
        if ($this->status == static::STATUS_CLOSE) {
            return 'default';
        }
        if ($this->status == static::STATUS_CANCEL) {
            return 'default';
        }
        if ($this->isPay) {
            if ($this->status >= static::STATUS_SHIP && $this->status <= static::STATUS_SHIPPED) {
                return 'warning';
            } elseif ($this->status >= static::STATUS_RECEIVED) {
                return 'default';
            } else {
                return 'warning';
            }
        }
        if (!$this->isExpired) {
            return 'warning';
        }
        return 'default';
    }

    /**
     * 订单商品件数
     * @return integer
     */
    public function getNums() {
        return (int) $this->getGoods()->sum('goods_number');
    }

    /**
     * 订单商品件数
     * @return integer
     */
    public function getStore() {
        if (!$this->getIsPay() && $this->unifiedOrderStores > 1) {
            return ShopStore::plateform();
        }
        return $this->hasOne(ShopStore::class, ['id' => 'store_id']);
    }

    /**
     * 订单应付金额
     * @return integer
     */
    public function getPayAmount() {
        if ($this->isPay) {
            return round($this->pay - $this->pay_credit / 10, 2);
        }
        return $this->unified->pay;
    }

    /**
     * 订单支付金额文本
     * @return string
     */
    public function getPayText() {
        if ($this->isPay) {
            $text = '实付';
            $amount = $this->pay - $this->pay_credit / 10;
        } else {
            $text = '需付款';
            $amount = $this->unified->pay;
        }
        return Yii::t('message', '{text}{amount}', [
            'text' => $text,
            'amount' => Yii::$app->formatter->asCurrency($amount),
        ]);
    }

    /**
     * 订单支付积分抵扣文本
     * @return string
     */
    public function getPayCredit() {
        if (!$this->isPay || !$this->pay_credit) {
            return '';
        }
        return Yii::t('message', '积分抵扣{amount}', [
            'amount' => Yii::$app->formatter->asCurrency($this->pay_credit / 10),
        ]);
    }

    /**
     * @inheritdoc
     */
    public function fields() {
        return [
            'id',
            'status_text' => 'statusText',
            'status_color' => 'statusColor',
            'store',
            'goods',
            'pay_text' => 'payText',
            'pay_credit' => 'payCredit',
            'nums',
            'handle',
            'mark',
            'weblink',
        ];
    }

}
